﻿#pragma once

#include "windows.h"
#include "FiberMgr.h"

#pragma warning(disable:4355)

template<typename T>
void CALLBACK ProcTemplate(PVOID arg)
{
	T *tasklet = static_cast<T*>(arg);
	tasklet->_run();
}

class noncopyable
{
protected:
	noncopyable() {}
	~noncopyable() {}
private:
	noncopyable( const noncopyable& );
	const noncopyable& operator=( const noncopyable& );
};

template<typename T> 
class Tasklet : public noncopyable
{
	friend void CALLBACK ProcTemplate<T>(PVOID arg);
public:
	Tasklet(T* obj)
	{
		FiberMgr::instance().createFiber(ProcTemplate<T>, obj);
	}

public:
	void yield()
	{
		FiberMgr::instance().switchToMainFiber();	
	}

	void yield(FiberMgr::FiberID fid)
	{
		FiberMgr::instance().switchToFiber(fid);
	}

protected:
	virtual ~Tasklet(void){}

	virtual void run() = 0;
private:
	void _run()
	{
		run();
		FiberMgr::instance().endFiber(GetCurrentFiber());
	}
};

